#Sugar libs
from sugar import profile
from sugar.graphics.xocolor import XoColor
import os

def change(filepath):
        """Changes stroke & fill from the .svg passed in to the user's colors."""
        stroke = sugar.profile.get_color().get_stroke_color()
        fill = sugar.profile.get_color().get_fill_color()
        try:
                #f is the reading file, so we can do read/write simultaneously
                f = open(filepath, 'r')
                #fi is the writing file
                fi = open(filepath+'!', 'w')
                #Get every line in f, scan it for stroke/fill and replace if found
                for line in f:
                        if line.find('stroke') != -1:
                                loc = line.find('#')
                                line = line.replace(stroke, line[loc+1:loc+7])
                        if line.find('fill') != -1:
                                loc = line.find('#')
                                line = line.replace(fill, line[loc+1:loc+7])
                        fi.write(line)
                #After writing to fi, change its name to f, thus deleting the old version
                os.system('mv %s %s' % (fi.name(), f.name()))
        except IOError:
                print "IOError...RUN FOR THE HILLS!"
        finally:
                f.close()
                fi.close()